<!DOCTYPE html>
<html lang="zh-cn" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head th:replace="common::head"></head>
<body>
<div th:replace="common::top"></div>
<br/>
<h2>订单列表</h2>
<a href="order/add.html" th:href="@{/order/add.html}"　 style="margin-left:-800px;">销售开单</a>
<br/>
<table style="width:900px;">
    <thead>
    <tr>
        <th>编号</th>
        <th>客户</th>
        <th>产线</th>
        <th>总量</th>
        <th>生产</th>
        <th>发货</th>
        <th>回款</th>
        <th>销售</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="item : ${page.getList()}" th:class="${item.status}==0?'del':''">
        <td><a th:href="@{detail.html(orderId=${item.getId()})}"
               th:text="${item.code}"></a></td>
        <td th:text="${item.customName}"></td>
        <td>
            <span th:if="${item.confirm}">已上线</span>
            <form th:if="!${item.confirm} and (${item.status}==1)"
                  th:action="@{upperLine}" method="post" class="upperLine">
                <input type="number" name="id" th:value="${item.id}" hidden>
                <input type="submit" value="上产线">
            </form>
        </td>
        <td th:text="${item.orderNum}" style="text-align: right;"></td>
        <td>
            <progress th:if="${item.confirm}" th:max="${item.orderNum}"
                      th:value="${item.produceNum}"></progress>
        </td>
        <td>
            <progress th:if="${item.confirm}" th:max="${item.orderNum}"
                      th:value="${item.sendNum}"></progress>
        </td>
        <td>
            <progress th:if="${item.confirm}" th:max="${item.contractAmount}"
                      th:value="${item.actualAmount}"></progress>
        </td>
        <td th:text="${item.createUser}"></td>
        <td>
            <a th:href="@{detail.html(orderId=${item.getId()})}">详情</a>
            <a th:href="@{edit.html(orderId=${item.getId()})}">编辑</a>
            <a th:href="@{batch.html(orderId=${item.getId()})}">产品</a>
            <a th:if="${item.status}!=0" href="javascript:void(0);" th:onclick="'cancel('+${item.id}+')'">撤销</a>
            <a th:if="${item.status}==0" href="javascript:void(0);" th:onclick="'resume('+${item.id}+')'">恢复</a>
            <a th:href="@{export(orderId=${item.getId()})}" download>导出</a>
        </td>
    </tr>
    </tbody>
</table>
<div>
    <a><b th:text="'第 '+${page.curPage}+'/'+${page.getLastPage()}+' 页'"></b></a>
    <a th:href="@{/order/list.html(page=${page.getFirstPage()})}">首页</a>
    <a th:if="${page.getPrePage()>0}"
       th:href="@{/order/list.html(page=${page.getPrePage()})}">上一页</a>
    <a th:if="${page.getNextPage()>0}"
       th:href="@{/order/list.html(page=${page.getNextPage()})}">下一页</a>
    <a th:href="@{/order/list.html(page=${page.getLastPage()})}">尾页</a>
</div>
<p><a href="../index.html" th:href="@{/index.html}">返回首页</a></p>
<script type="text/javascript" th:inline="javascript">
var csrfToken = [[${_csrf.token}]];

$(".upperLine").submit(function(e){
    e.preventDefault();

    if(confirm("确认订单内容并上产线生产!")){
        $.ajax({
            type: $(e.target).attr("method"),
            url: $(e.target).attr("action"),
            data: $(e.target).serialize(),
            success: function(response){
                //alert("上线成功");
                window.location.reload();
            },
            error: function(response){
                alert("提交失败");
            }
        });
    }
});

function cancel(orderId){
    if(confirm("确认撤销订单!")){
        $.post(
            "cancel",
            {orderId:orderId,_csrf:csrfToken}
        ).done(function(data){
            //alert("撤销成功");
            window.location.reload();
        });
    }
}

function resume(orderId){
    if(confirm("确认恢复订单!")){
        $.post(
            "resume",
            {orderId:orderId,_csrf:csrfToken}
        ).done(function(data){
            //alert("恢复成功");
            window.location.reload();
        });
    }
}
</script>
</body>
</html>
